﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;
using Ongoal.Quotation;
using Ongoal.Quotation.Service.Interface;
using System.Dynamic;
using System.Text;

namespace Ongoal.Quotation.WebAPI.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("Doc")]
    public class DocsController : WebAPIBase
    {
        private IDocService _mongoRepo;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repo"></param>
        public DocsController(IDocService repo)
        {
            _mongoRepo = repo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        [HttpGet("{tableName}/{id}")]
        public object GetAll(string tableName, [FromRoute] string id)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("表名不可为空");
            }
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("id不可为空");
            }
            return _mongoRepo.GetOne(tableName, id);
        }

        /// <summary>
        /// 获取表的数据集合
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns>数据集合</returns>
        /// <exception cref="ArgumentException"></exception>
        [HttpGet("{tableName}")]
        public object GetAll(string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("表名不可为空");
            }
            return _mongoRepo.GetAll(tableName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        [HttpPost]
        [Route("search")]
        //public object Search([FromBody] string tableName, [FromBody] int size, [FromBody] int index, [FromBody] string searchJson)
        public object Search([FromBody] dynamic info)
        {
            var tableName = Convert.ToString(info.tableName);
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("表名不可为空");
            }
            var size = Convert.ToInt32(info.size);
            var index = Convert.ToInt32(info.index);
            var searchJson = Convert.ToString(info.searchJson);
            //return info;
            return _mongoRepo.Search(tableName, size, index, searchJson);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        [HttpPost("{tableName}")]
        public dynamic Post(string tableName, [FromBody] Newtonsoft.Json.Linq.JObject doc)
        {
            return _mongoRepo.Insert(tableName, BsonDocument.Parse(Convert.ToString(doc["content"])));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        [HttpPut("{tableName}")]
        public dynamic Put(string tableName, [FromBody] Newtonsoft.Json.Linq.JObject doc)
        {
            return _mongoRepo.Update(tableName, BsonDocument.Parse(Convert.ToString(doc["content"])));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [HttpDelete("{tableName}")]
        public dynamic Delete(string tableName, [FromBody] List<string> IdList)
        {
            return _mongoRepo.Delete(tableName, IdList);
        }


    }
}
